package a66weding.com.jiehuntong.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;

import org.xutils.x;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.MyTaskBean;
import a66weding.com.jiehuntong.beans.MyTaskNewBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.common.utils.dataselect.DataSelectUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
import a66weding.com.jiehuntong.widgets.CircularImage;

/**
 * Created by admin on 2016/7/7.
 */
public class MyTaskAdapter extends BaseRecyclerAdapter<MyTaskAdapter.ViewHolder> {
    //如果有点击事件请设置一个监听;
    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private Context context;
    private List<MyTaskNewBean.DataBean> data;
    public MyTaskAdapter(Context context, List<MyTaskNewBean.DataBean> data){
        this.context = context;
        if (data != null) {
            this.data = data;
        }
    }
    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }
    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(context).inflate(
                R.layout.item_activity_mytask, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    //作绑定数据用的;
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position, boolean isItem) {
        if (mOnItemClickLitener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView,pos);
                }
            });
        }
        if(data!=null){
            //头像
            if (data.get(position).getPicture()!=null) {
                x.image().bind(holder.my_task_icon, "" + data.get(position).getPicture(), ImageOptionsUtil.getOptions92_92());
                x.image().bind(holder.item_activity_my_task_worker_ic, "" + data.get(position).getPicture(), ImageOptionsUtil.getOptions92_92());
            }
            //任务
            holder.task_myTask.setText(data.get(position).getBookTitle());
            //金额
            holder.money.setText("¥"+data.get(position).getBudgetMoney());
            //名字
            if (!TextUtils.isEmpty(data.get(position).getNvc_RealName())){
                holder.name.setText(data.get(position).getNvc_RealName());
            }else{
                if (TextUtils.isEmpty(data.get(position).getNvc_Name())){
                    holder.name.setText(data.get(position).getNvc_Name());
                }
            }
            holder.item_activity_my_task_last_day.setText(""+data.get(position).getFinishTime());
            if (data.get(position).isIsComplete()){
                holder.isComplete.setText("已完成");
                holder.isComplete.setTextColor(context.getResources().getColor(R.color.colorMainRed));
            }

            String days = data.get(position).getDays();
            if (!TextUtils.isEmpty(days)&&!"null".equals(days)){
                if ("0".equals(days)){
                    holder.item_activity_my_task_last_day.setText("今天");
                }else if (days.contains("-")){
                    holder.item_activity_my_task_last_day.setText(days);
                }else {
                    holder.item_activity_my_task_last_day.setText("距离还有"+days+"天");
                }
            }else{
                holder.item_activity_my_task_last_day.setText("--");
            }
            Log.e("-----------","*******"+data.get(position));
            String bookDays = data.get(position).getBookDate();
            holder.data.setText(bookDays);
        }


    }
    public void append(List<MyTaskNewBean.DataBean> data) {
        if (this.data != null) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }
        notifyDataSetChanged();
    }
    @Override
    public int getAdapterItemCount() {
        if (data ==null){
            return 0;
        }
        return  data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        //时间
        private TextView item_activity_my_task_last_day;
        //头像的图标
        private CircularImage my_task_icon;
        //任务
        private TextView task_myTask;
        //金额
        private TextView money;
        //剩余天数
        private TextView day;
        //名字
        private TextView name;
        //日我
        private TextView data;
        //是否完成任务
        private TextView isComplete;
        //底部头像
        private CircularImage item_activity_my_task_worker_ic;
        public ViewHolder(View itemView) {
            super(itemView);
            my_task_icon = (CircularImage) itemView.findViewById(R.id.item_activity_my_task_circle);
            task_myTask = (TextView) itemView.findViewById(R.id.item_activity_my_task_myTask);
            money = (TextView) itemView.findViewById(R.id.item_activity_my_task_money);
            day = (TextView) itemView.findViewById(R.id.item_activity_my_task__day);
            name = (TextView) itemView.findViewById(R.id.item_activity_my_task_worker_name);
            data = (TextView) itemView.findViewById(R.id.item_activity_my_task_worker_data);
            isComplete = (TextView) itemView.findViewById(R.id.item_activity_my_task_isComplete);
            item_activity_my_task_worker_ic = (CircularImage) itemView.findViewById(R.id.item_activity_my_task_worker_ic);
            item_activity_my_task_last_day = (TextView) itemView.findViewById(R.id.item_activity_my_task_last_day);
        }
    }
}
